﻿using LogLibrary.Entity;
using SilverPOP.Data.Process.BLL;
using SilverPOP.Data.Process.Entity;
using SilverPOP.Data.Process.Helpers.LoggerHelper;
using SilverPOP.Data.Process.OperationUnit;
using System;
using System.Collections.Generic;
using System.Threading;

namespace SilverPOP.Data.Process
{
    public class TaskList
    {
        List<TaskListEntity> _tasks;

        public bool SetTaskList()
        {
            _tasks = new TaskListBLL().GetAllActiveTask();

            if (_tasks == null)
                return false;

            if (_tasks.Count > 0)
                return true;
            else
                return false;
        }

        public void ExecuteTaskList()
        {
            DataAndSchemaProcess process = null;

            foreach (var task in _tasks)
            {
                Console.WriteLine("Start Task : "+task.ProjectName +" at "+Convert.ToString(DateTime.Now) );
                try
                {
                new TaskListBLL().UpdateTaskTableBasedOnOperationState(task.TaskListId, task.CampaignId, false);

                LoggerHelper.ProjectId = task.ProjectId;
                LoggerHelper.TaskListId = task.TaskListId;
                LoggerHelper.CampaignId = task.CampaignId;

             
                if (task.TaskType == TaskType.CreateSchema.ToString())
                {
                    process = new AddCustomColumnProcess(task);
                }
                else if (task.TaskType == TaskType.DataSyncWithContactList.ToString())
                {
                    process = new DataSyncProcess(task);
                }
                else if (task.TaskType == TaskType.DataSyncWithOutContactList.ToString())
                {
                    process = new DataSyncProcessWithOutContactList(task);
                }
                else if (task.TaskType == TaskType.UpdateData.ToString())
                {
                    process = new DataUpdateProcess(task);
                }
                else if (task.TaskType == TaskType.RawReport.ToString())
                {
                    process = new SilverPOPRawReportProcess(task);
                }
                else if (task.TaskType == TaskType.RawRoportSummary.ToString())
                {
                    process = new SilverPOPAggregateReportProcess(task);
                }

               
                    process.ExecuteOperation();
                  

                    new TaskListBLL().UpdateTaskTableBasedOnOperationState(task.TaskListId,task.CampaignId,true );
                }
                catch (Exception exception)
                {
                    var logInfo = new LogServiceInformation
                    {
                        Exception = exception,
                        StackTrace = Environment.StackTrace,
                    };
                    LoggerHelper.Log(logInfo, typeof(Program), LogType.ERROR);
                }

                Console.WriteLine("End Task : " + task.ProjectName +" at "+Convert.ToString(DateTime.Now) );
            }
        }
    }
}